# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libarchive
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.1
pkgrel=1
pkgdesc="Multi-format archive and compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.libarchive.org/"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-bzip2
         ${MINGW_PACKAGE_PREFIX}-expat
         ${MINGW_PACKAGE_PREFIX}-libiconv
         ${MINGW_PACKAGE_PREFIX}-lz4
         ${MINGW_PACKAGE_PREFIX}-libsystre
         ${MINGW_PACKAGE_PREFIX}-nettle
         ${MINGW_PACKAGE_PREFIX}-openssl
         ${MINGW_PACKAGE_PREFIX}-xz
         ${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-zstd)
options=('!libtool' 'strip')
source=("https://github.com/libarchive/libarchive/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz"
        "0001-libarchive-3.3.3-bcrypt-fix.patch")
sha256sums=('0e17d3a8d0b206018693b27f08029b598f6ef03600c2b5d10c94ce58692e299b'
            '2c318a025029998a9389eb99ab80f733c0fcf3b4888421879f2f6b4530d7f522')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-libarchive-3.3.3-bcrypt-fix.patch
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --prefix="${MINGW_PREFIX}" \
    --without-xml2 \
    --without-lzo2
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
 
  #fix issue with hard-coded path in libarchive.pc file.
  _localpath=$(cygpath -wm ${MINGW_PREFIX})
  sed -i -e "s|${_localpath}|${MINGW_PREFIX}|g" ${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libarchive.pc
}
